﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using docNET.Common;
using System.Xml.Serialization;
using System.IO;
using docNET.Controls;
using docNET.Resources;

namespace docNET
{
	/// <summary>
	/// docNet windows form.
	/// </summary>
	public partial class docNetForm : Form
	{
		#region C'tor
		/// <summary>
		/// Initializes a new instance of the docNet windows form.
		/// </summary>
		public docNetForm()
		{
			InitializeComponent();
			Text = Helper.GetAppName(true);
		}
		#endregion

		#region Properties
		#endregion

		#region Methods
		/// <summary>
		/// Creates a new project
		/// </summary>
		private void CreateProject()
		{
			FileHandler.PrepareSaveFileDialog(ref saveFileDialog, FileHandler.FileType.Project);

			if (saveFileDialog.ShowDialog() != DialogResult.OK)
				return;

			try
			{
				Current.project = new Project(saveFileDialog.FileName);
				Current.project.ProjectFiles.Add("Hello.file");
				FileHandler.SaveProject();
				Current.item = Path.GetFileName(Current.project.ProjectFile);
				Text = string.Format("{0} - {1}", Current.project.Name, Helper.GetAppName(true));
			}
			catch (Exception ex)
			{
				DnMessageBox.Show(Messages.ErrorCreateProjectFailed, MessageBoxIcon.Error, ex);
				UnloadProject(true);
			}
		}

		private void OpenProject()
		{
			FileHandler.PrepareOpenFileDialog(ref openFileDialog, FileHandler.FileType.Project);

			if (openFileDialog.ShowDialog() != DialogResult.OK)
				return;

			try
			{
				FileHandler.OpenProject(openFileDialog.FileName);
				Current.item = Path.GetFileName(Current.project.ProjectFile);
				Text = string.Format("{0} - {1}", Current.project.Name, Helper.GetAppName(true));
				GrowProjectTree();
			}
			catch (Exception ex)
			{
				DnMessageBox.Show(Messages.ErrorCreateProjectFailed, MessageBoxIcon.Error, ex);
			}
		}

		/// <summary>
		/// Unloads current project.
		/// </summary>
		/// <param name="quick">Do a quick unload.</param>
		private void UnloadProject(bool quick)
		{
			if (!quick)
				FileHandler.SaveProject();

			Current.project = null;
			Current.item = null;
			Text = Helper.GetAppName(true);
		}

		/// <summary>
		/// Fills the project tree view.
		/// </summary>
		private void GrowProjectTree()
		{
			Project p = Current.project;

			TreeNode root=projectTreeView.Nodes.Add(p.Name);

			foreach (string f in p.ProjectFiles)
				root.Nodes.Add(Path.GetFileName(f));
		}
		#endregion

		#region Form Events
		private void exitToolStripMenuItemClick(object sender, EventArgs e)
		{
			//TODO Add checking for dirty items when exiting app.
			Close();
		}

		private void saveToolStripMenuItemClick(object sender, EventArgs e)
		{
			FileHandler.SaveProject();
			messageToolStripStatusLabel.Text = string.Format("Item {0} saved.", Current.item);
		}

		private void saveAsToolStripMenuItemClick(object sender, EventArgs e)
		{
			messageToolStripStatusLabel.Text = string.Format("Item {0} saved.", Current.item);
		}

		private void newProjectToolStripMenuItemClick(object sender, EventArgs e)
		{
			CreateProject();
			messageToolStripStatusLabel.Text = string.Format("Item {0} created.", Current.item);
		}

		private void openProjectToolStripMenuItemClick(object sender, EventArgs e)
		{
			OpenProject();
			messageToolStripStatusLabel.Text = string.Format("Item {0} loaded.", Current.item);
		}

		private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OptionsForm o = new OptionsForm();
			o.ShowDialog();
		}

		private void fileToolStripMenuItemDropDownOpening(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(Current.item))
			{
				saveToolStripMenuItem.Text = DynamicItems.FileSaveNoItem;
				saveAsToolStripMenuItem.Text = DynamicItems.FileSaveNoItemAs;
			}
			else
			{
				saveToolStripMenuItem.Text = string.Format(@DynamicItems.FileSaveItem, Current.item);
				saveAsToolStripMenuItem.Text = string.Format(@DynamicItems.FileSaveItemAs, Current.item);
			}

			saveToolStripMenuItem.Enabled = saveAsToolStripMenuItem.Enabled =
				closeToolStripMenuItem.Enabled = !string.IsNullOrEmpty(Current.item);

			closeProjectToolStripMenuItem.Enabled = Current.project != null;
		}
		#endregion

		private void closeProjectToolStripMenuItemClick(object sender, EventArgs e)
		{
			UnloadProject(false);
		}
	}
}
